package wwk.common.util;

import java.util.ArrayList;
import java.util.List;
import wwk.common.http.ApnNode;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

/**
 * @author wangweike
 * @create_date 2011-8-3����1:32:27
 * @update_date 2011-8-3����1:32:27
 */

public class ApnUtil
{
	private static String Telephony_Carriers="content://telephony/carriers";
	private static String Telephony_Carriers_PREFERAPN="content://telephony/carriers/preferapn";
	
	public static List<ApnNode> getApnNodeList(Context context) 
	{
		try 
		{
			List<ApnNode> list=new ArrayList<ApnNode>();
			ContentResolver cr = context.getContentResolver();
			final Uri APN_URI = Uri.parse(Telephony_Carriers);
			Cursor cursor=cr.query(APN_URI, 
					new String[]{ ApnNode.ID,
					ApnNode.NAME,
					ApnNode.APN,
					ApnNode.PROXY,
					ApnNode.PORT,
					ApnNode.USER,
					ApnNode.SERVER,
					ApnNode.PASSWORD,
					ApnNode.MMSC,
					ApnNode.MMSPROXY,
					ApnNode.MMSPORT,
					ApnNode.MCC,
					ApnNode.MNC,
					ApnNode.NUMERIC,
					ApnNode.TYPE},
					null, null, null);
			int count=cursor.getCount();
			Log.d("ApnUtil",""+count);
			while (cursor != null && cursor.moveToNext())
		    {
				ApnNode node=newApnNode(cursor);
				
				list.add(node);
		    }
			return list;
		}
		catch (Exception ep)
		{
			Log.e("getApnNodeList", ep.getMessage());
			return null;
		}
	}
	
	private static ApnNode getPrederApn(Context context)
	{
		ApnNode apnNode=null;
		
		//ʹ�ò�ѯ
		final Uri PREFERRED_APN_URI = Uri.parse(Telephony_Carriers_PREFERAPN);
		ContentResolver cr = context.getContentResolver();
		Cursor cursor=cr.query(PREFERRED_APN_URI, 
				new String[]{ ApnNode.ID,
								ApnNode.NAME,
								ApnNode.APN,
								ApnNode.PROXY,
								ApnNode.PORT,
								ApnNode.USER,
								ApnNode.SERVER,
								ApnNode.PASSWORD,
								ApnNode.MMSC,
								ApnNode.MMSPROXY,
								ApnNode.MMSPORT,
								ApnNode.MCC,
								ApnNode.MNC,
								ApnNode.NUMERIC,
								ApnNode.TYPE},
			null,null, null);
		if (cursor != null && cursor.moveToNext())
	    {
			apnNode=newApnNode(cursor);
	    }

		return apnNode;
	}

	public static ApnNode getApn(Context context,int apnId)
	{
		ApnNode apnNode=null;
		
		if(apnId==0)
		{
			apnNode=getPrederApn(context);
		}
		else
		{
			//ʹ�ò�ѯ
			final Uri APN_URI = Uri.parse(Telephony_Carriers);
			ContentResolver cr = context.getContentResolver();
			Cursor cursor=cr.query(APN_URI, 
					new String[]{ ApnNode.ID,
									ApnNode.NAME,
									ApnNode.APN,
									ApnNode.PROXY,
									ApnNode.PORT,
									ApnNode.USER,
									ApnNode.SERVER,
									ApnNode.PASSWORD,
									ApnNode.MMSC,
									ApnNode.MMSPROXY,
									ApnNode.MMSPORT,
									ApnNode.MCC,
									ApnNode.MNC,
									ApnNode.NUMERIC,
									ApnNode.TYPE},
				ApnNode.ID+"="+apnId,null, null);//"_id=?",new String[]{""+apnId}, null);
			if (cursor != null && cursor.moveToNext())
		    {
				apnNode=newApnNode(cursor);
		    }
			
			if(apnNode==null)
			{
				apnNode=getPrederApn(context);
			}
		}

		return apnNode;
	}
	
	private static ApnNode newApnNode(Cursor cursor)
	{
		long id = 		cursor.getLong(0);
		String name = 	cursor.getString(1);
		String apn = 	cursor.getString(2);
		String proxy = 	cursor.getString(3);
		int port = 		cursor.getInt(4);
		
		String user = 	cursor.getString(5);
		String server = 	cursor.getString(6);
		String password = 	cursor.getString(7);
		String mmsc = 	cursor.getString(8);
		String mmsproxy = 	cursor.getString(9);
		String mmsport = 	cursor.getString(10);
		String mcc = 	cursor.getString(11);
		String mnc = 	cursor.getString(12);
		String numeric = 	cursor.getString(13);
		String type = 	cursor.getString(14);
		
		ApnNode node=new ApnNode();
		node.setId(id);
		node.setName(name);
		node.setApn(apn);
		node.setProxy(proxy);
		node.setPort(port);
		
		return node;
	}
}
